#include <iostream>

#include "CSync.h"

using namespace std;

void parent_do_something()
{
	for(int i=0;i<5;i++) {
		cout << "[p]I'm busy now " << i << endl;
		sleep(3);
	}
}

void child_do_something()
{
	for(int i=0;i<5;i++) {
		cout << "[c]i'm sleeping now " << i << endl;
		sleep(2);
	}
}

int main(int argc, char **argv)
{
	CSync sync;

	pid_t pid;
	if((pid=fork())<0) cerr << "fork error" << endl;
	else if(pid==0) { // child
		cout << "[c]start child process " << getpid() << endl;

		child_do_something();

		sync.tell_parent(getppid());
		cout << "[c]wait parent process " << getppid() << endl;
		sync.wait_parent();
		cout << "[c]exit child process " << getpid() << endl;
	
		exit(0);
	}

	// parent
	cout << "[p]start parent process " << getpid() << endl;

	parent_do_something();

	sync.tell_child(pid);
	cout << "[p]wait child process " << pid << endl;
	sync.wait_child();
	cout << "[p]exit parent process " << getpid()<< endl;

	exit(0);
}
